package com.lightsh.body;

import java.util.ArrayList;
import org.jbox2d.dynamics.World;

import com.lightsh.interfaces.creator.IActionCreator;
import com.lightsh.interfaces.creator.IBodyCreator;

public abstract class PhysicsBody extends BaseBody implements IBodyCreator{
	protected final World mWorld;
	
	private ArrayList<IActionCreator> mActions;
	/**
	 * 需要记录本body所属screen
	 */
	public PhysicsBody(World world){
		mWorld = world;
		mActions = new ArrayList<IActionCreator>();
	}
	/**
	 * 添加action到body的队列中,body会在{@link #create()}或者下一个{@link #update()}时调用{@link IActionCreator#doAction()}方法来初始化action
	 */
	public void addAction(IActionCreator action){
		synchronized (mActions) {
			mActions.add(action);
		}
	}
	
	@Override
	public void update() {
		super.update();
		applyAction();
	}

	/**
	 * 初始化所有设置的ation操作
	 */
	protected void applyAction(){
		synchronized (mActions) {
			if(!mActions.isEmpty()){
				for(IActionCreator action : mActions){
					action.doAction();
				}
				mActions.clear();
			}
		}
	}
	@Override
	public void create() {
		if(mBody != null){
			throw new RuntimeException("body had been created!");
		}
		
	}
	
	
}
